# !/usr/bin/env python
# -*-coding:utf-8 -*-
"""
# File       : process.py
# Time       ：2023/7/7 21:48
# Author     ：Y-aong
# version    ：python 3.7
# Description：
    process 模型类
    process model class
"""
import json

from sqlalchemy import func

from public.base_model import Base, db

default_process_params = json.dumps({'is_send': True, 'notice_type': "FAILURE", 'timeout': 7201})


class Process(Base):
    __tablename__ = 'base_process'

    process_id = db.Column(db.String(255), unique=True, comment='流程ID')
    process_name = db.Column(db.String(30), nullable=False, comment='流程名称')
    version = db.Column(db.String(128), default='默认版本', comment='流程版本')
    version_desc = db.Column(db.String(255), comment='版本描述')
    process_params = db.Column(db.JSON, comment='流程参数', default=default_process_params)
    process_config = db.Column(db.JSON, comment='其他配置')
    desc = db.Column(db.String(255), comment='流程描述')
    mode = db.Column(db.String(10), default='running', comment='流程运行方式，debug或者running')
    namespace = db.Column(db.String(50), default='default', comment='流程的命名空间')
    # 关联关系
    process_instance = db.relationship('ProcessInstance', backref='base_process')
    task = db.relationship('Task', backref='base_process')
    task_instance = db.relationship('TaskInstance', backref='base_process')


class ProcessInstance(Base):
    __tablename__ = 'base_process_instance'

    process_instance_id = db.Column(db.String(255), unique=True, comment='流程运行实例ID')
    process_name = db.Column(db.String(30), comment='流程名称')
    version = db.Column(db.String(128), default='默认版本', comment='流程版本')
    version_desc = db.Column(db.String(255), comment='版本描述')
    process_params = db.Column(db.JSON, comment='流程运行实例参数')
    process_config = db.Column(db.JSON, comment='流程运行实例配置')
    start_time = db.Column(db.DateTime, default=func.now(), comment='流程运行实例开始时间')
    end_time = db.Column(db.DateTime, comment='流程运行实例结束时间')
    runner = db.Column(db.String(40), comment='流程运行实例运行者')
    runner_id = db.Column(db.String(40), comment='流程运行实例运行者ID')
    process_error_info = db.Column(db.JSON, comment='流程错误信息')
    process_status = db.Column(db.String(10), comment='流程运行实例状态')
    mode = db.Column(db.String(10), comment='流程运行模式。debug或running', default='running')
    namespace = db.Column(db.String(50), default='default', comment='流程的命名空间')
    # 关联关系
    task_instance = db.relationship('TaskInstance', backref='base_process_instance')
    process_id = db.Column(db.String(255), db.ForeignKey('base_process.process_id'), comment='流程ID')
    run_type = db.Column(db.Enum('schedule', 'trigger'), default='trigger', comment='触发方式')
